﻿using UnityEngine;
using System.Collections;

public class WeaponScript : MonoBehaviour {

	public Transform shotPrefab;

	public float shootingRate = 0.25f;

	private float shotCooldown;

	void Start()
	{
		shotCooldown = 0f;
	}

	void Update()
	{
		if (shotCooldown > 0) {
			shotCooldown -= Time.deltaTime;
		}
	}

	public void Attack(bool isEnemy)
	{
		if (CanAttack) {
			shotCooldown = shootingRate;

			var shotTransform = Instantiate (shotPrefab) as Transform;

			shotTransform.position = transform.position;

			ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript> ();
			if (shot != null)
				shot.isEnemyShot = isEnemy;

			MovementController move = shotTransform.gameObject.GetComponent<MovementController> ();
			if (move != null)
				move.direction = this.transform.up;

		}
	}

	public bool CanAttack
	{
		get{
			return shotCooldown <= 0f;
		}
	}

}
